/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.table.TableColumn;

/**
 *
 * @author HoangLiem
 */
public class ReadSelectInfo extends Thread {

    private Socket socket;
    private JTabbedPane jTpTableData;
    private JTextArea textArea;
    private JTable table;

    public ReadSelectInfo(Socket socket, JTabbedPane jTpTableData, JTextArea textArea, JTable table) {
        this.socket = socket;
        this.jTpTableData = jTpTableData;
        this.textArea = textArea;
        this.table = table;
    }

    void initDataTable(ArrayList<ArrayList<String>> dataFromServer) {
        ArrayList<String> columnNames = new ArrayList<String>();
        ArrayList<ArrayList<Object>> data = new ArrayList<ArrayList<Object>>();
        if (dataFromServer != null) {
            columnNames = dataFromServer.get(0);
            if (dataFromServer.size() > 1) {
                for (int i = 1; i < dataFromServer.size(); i++) {
                    ArrayList<Object> objs = new ArrayList<Object>();
                    ArrayList<String> rowData = dataFromServer.get(i);
                    if (rowData != null && !rowData.isEmpty()) {
                        for (int r = 0; r < rowData.size(); r++) {
                            Object obj = new String(rowData.get(r));
                            objs.add(obj);
                        }
                    }
                    data.add(objs);
                }
            }
            this.jTpTableData.setSelectedIndex(1);
        } else {
            this.jTpTableData.setSelectedIndex(0);
            textArea.append(", No data selected");
        }
        DataTableModel model = new DataTableModel(columnNames, data);
        table.setModel(model);
        initColumnSizes(table, columnNames);
    }

    private void initColumnSizes(JTable table, ArrayList<String> columnNames) {
        DataTableModel model = (DataTableModel) table.getModel();
        TableColumn column = null;
        int headerWidth = 120;
        for (int i = 0; i < model.getColumnCount(); i++) {
            column = table.getColumnModel().getColumn(i);
            headerWidth = columnNames.get(i).length() * 10 + 20;
            column.setPreferredWidth(headerWidth);
        }
        table.setRowHeight(18);
    }

    public void run() {
        ObjectInputStream ois;
        try {
            ois = new ObjectInputStream(socket.getInputStream());
            ArrayList<ArrayList<String>> dataFromServer = (ArrayList<ArrayList<String>>) ois.readObject();
            initDataTable(dataFromServer);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ReadSelectInfo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ReadSelectInfo.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
